AOP注解形式 整合memcache

您所在的位置:网站首页 enablecache 注解 AOP注解形式 整合memcache

AOP注解形式 整合memcache

#AOP注解形式 整合memcache| 来源: 网络整理| 查看: 265

1.首先自定义注解 :添加缓存

@Target(ElementType.METHOD)@Retention(RetentionPolicy.RUNTIME)@Documented@Inheritedpublic @interface Memcached { // key的前缀 default=STATIC的可以使用OMS清理缓存 String prefix() default "STATIC_"; // key String key() default ""; // 过滤 String conditions() default "true"; // 緩存分組 String group() default "hos-portal-mclient1"; // 缓存有效期 2天 单位s int expiration() default 60 * 60 * 48;}2.切面类

@Aspectpublic class MemCachedAop { private static final Logger log = LoggerFactory.getLogger(MemCachedAop.class); @Value("${enable.static.cache}") private boolean enableCache; @Pointcut("execution(* com.ylzinfo.hospital.portal.service.appservice.*.*.*(..))") protected void appservicePointcut(){} @Autowired private ICacheClient cacheClient; /** * 写入或者读取缓存 * 仅针对有注解的且该包下的方法 */ @Around("(@annotation(memcached) && appservicePointcut())") public Object doMemcachedAround(ProceedingJoinPoint call, Memcached memcached) throws Throwable { String packageName = call.getSignature().getDeclaringTypeName(); String methodName = call.getSignature().getName(); log.info("执行方法: {} -> {}", packageName, methodName); //返回最终结果 Object result = null; //校验conditions if (null != memcached && checkConditions(call,memcached.conditions()) && enableCache) { String key = resolvingKey(call,memcached.prefix(),memcached.key()); String group = memcached.group(); result = cacheClient.get(group, key); if (null == result) { // memcached中不存在 try { //执行aop拦截的方法 result = call.proceed(); //获取注解配置memcached过期时间 int expiration = memcached.expiration(); cacheClient.put(group, key, result, expiration); log.info("\n【写入Memcached缓存】" + "\ngroup={}" + "\nkey={}" + "\nvalue={}" + "\nexpiration={}",group,key, JSON.toJSON(result),expiration); } catch (Throwable e) { log.error("执行方法失败: {} -> {}", packageName, methodName); log.error("失败原因:{}",e.getMessage()); } }else{ // memcached中存在 直接返回 log.info("\n【读取Memcached缓存】" + "\ngroup={}" + "\nkey={}" + "\nvalue={}",group,key,JSON.toJSON(result)); } }else { try { result = call.proceed(); } catch (Throwable e) { log.error("执行方法失败: {} -> {}", packageName, methodName); log.error("失败原因:{}",e.getMessage()); } } return result; } /** * 获取缓存的key * key 定义在注解上,支持SPEL表达式 * @param key * @param method * @param args * @return */ private String parseKey(String key, Method method, Object [] args){ //获取被拦截方法参数名列表(使用Spring支持类库) LocalVariableTableParameterNameDiscoverer u = new LocalVariableTableParameterNameDiscoverer(); String [] paraNameArr=u.getParameterNames(method); //使用SPEL进行key的解析 ExpressionParser parser = new SpelExpressionParser(); //SPEL上下文 StandardEvaluationContext context = new StandardEvaluationContext(); //把方法参数放入SPEL上下文中 for(int i=0;i



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3